from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings

def generc_email_token(email,user_id):
    #创建实例对象
    s = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)
    #组织数据
    data = {
        'email':email,
        'user_id':user_id
    }
    #进行加密
    secret_data = s.dumps(data)
    #返回二进制后的加密数据
    return secret_data.decode()

from itsdangerous import BadSignature
def check_email_token(token):
    s = Serializer(secret_key='abc', expires_in=3600)

    try:
        data = s.loads(token)
    except BadSignature:
        return None
    else:
        user_id = data.get('user_id')

        return user_id
